package com.xja.dubbo.service.impl;

import com.xja.dubbo.entity.Easybuy_product_category;
import com.xja.dubbo.mapper.Easybuy_product_categoryMapper;
import com.xja.dubbo.service.ProductCategoryService;
import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author ycz
 * @create 2021-12-15-10:17
 */
@Component
@Service(interfaceClass = ProductCategoryService.class)
public class ProductCategoryServiceImpl implements ProductCategoryService {

    private Logger logger = LoggerFactory.getLogger(ProductCategoryServiceImpl.class);

    @Autowired
    private Easybuy_product_categoryMapper easybuy_product_categoryMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Easybuy_product_category> findCategoryByPid(Integer pid) throws Exception {

        if(redisTemplate.boundHashOps("categoryList").get("categoryList_"+pid)!=null){
            List<Easybuy_product_category> category =(List<Easybuy_product_category>) redisTemplate.boundHashOps("categoryList").get("categoryList_"+pid);
            logger.info("从redis中取出"+category);
            return category;
        }
        List<Easybuy_product_category> category = easybuy_product_categoryMapper.findCategoryByPid(pid);

        logger.info("从数据库中取出"+category);
        redisTemplate.boundHashOps("categoryList").put("categoryList_"+pid, category);
        return category;

    }
}
